<template>
  <div class="search-history">
    <van-cell title="搜索历史">
      <div v-if="isDeleteShow">
        <span @click="$emit('clear-search-history', [])">全部删除 </span>
        <span @click="isDeleteShow = false"> 完成</span>
      </div>
      <van-icon name="delete" v-else @click="isDeleteShow = true"></van-icon>
    </van-cell>
    <van-cell :title="item" v-for="(item, index) in searchHistories" :key="index" @click="onSearchItemClick(item, index)">
      <van-icon name="close" v-show="isDeleteShow"></van-icon>
    </van-cell>
  </div>
</template>

<script>
export default {
  name: 'SearchHistory',
  props: {
    searchHistories: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      isDeleteShow: false // 控制删除展示状态
      // searchHistory: []
    }
  },
  methods: {
    onSearchItemClick(item, index) {
      this.searchHistory = this.searchHistories
      // console.log(1)
      if (this.isDeleteShow) {
        // 删除状态 清除历史记录数据
        this.$emit('delete-search-item', index)
      } else {
        // 非删除状态 直接进入搜索
        this.$emit('search', item)
        // console.log(item)
      }
    }
  }
}
</script>

<style lang="less" scoped>
</style>
